package com.cxj.link.view;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import android.graphics.Point;

import com.cxj.link.vo.GameConfig;
import com.cxj.link.vo.LinkVo;

public class GameLogicImpl implements GameLogic {
    protected AbstractBoard board = null;
    protected GameConfig config;
    protected Cut[][] pieces;

    public GameLogicImpl(GameConfig paramGameConf) {
        this.config = paramGameConf;
    }

    private int countAll(List<Point> paramList) {
        int i = 0;
        for (int j = 0; j < paramList.size() - 1; j++)
            i += getDistance(paramList.get(j), paramList.get(j + 1));
        return i;
    }

    private Point getCornerPoint(Point paramPoint1, Point paramPoint2, int paramInt1, int paramInt2) {
        Object localObject1;
        if ((!isLeftUp(paramPoint1, paramPoint2)) && (!isLeftDown(paramPoint1, paramPoint2))) {
            List localList2 = getRightChanel(paramPoint1, paramPoint2.x, paramInt1);
            localObject1 = getUpChanel(paramPoint1, paramPoint2.y, paramInt2);
            List localList4 = getDownChanel(paramPoint1, paramPoint2.y, paramInt2);
            List localList3 = getDownChanel(paramPoint2, paramPoint1.y, paramInt2);
            List localList1 = getLeftChanel(paramPoint2, paramPoint1.x, paramInt1);
            Object localObject2 = getUpChanel(paramPoint2, paramPoint1.y, paramInt2);
            if (!isRightUp(paramPoint1, paramPoint2)) {
                if (!isRightDown(paramPoint1, paramPoint2)) {
                    localObject1 = null;
                } else {
                    localObject1 = getWrapPoint(localList4, localList1);
                    localObject2 = getWrapPoint(localList2, (List) localObject2);
                    if (localObject1 != null)
                        localObject1 = localObject1;
                    else
                        localObject1 = localObject2;
                }
            } else {
                localObject2 = getWrapPoint(localList2, localList3);
                localObject1 = getWrapPoint((List) localObject1, localList1);
                if (localObject2 != null)
                    localObject1 = localObject2;
                else
                    localObject1 = localObject1;
            }
        } else {
            localObject1 = getCornerPoint(paramPoint2, paramPoint1, paramInt1, paramInt2);
        }
        return (Point) localObject1;
    }

    private int getDistance(Point paramPoint1, Point paramPoint2) {
        return Math.abs(paramPoint1.x - paramPoint2.x) + Math.abs(paramPoint1.y - paramPoint2.y);
    }

    private List<Point> getDownChanel(Point paramPoint, int paramInt1, int paramInt2) {
        ArrayList localArrayList = new ArrayList();
        int i = paramInt2 + paramPoint.y;
        while (true) {
            if ((i > paramInt1) || (hasPiece(paramPoint.x, i)))
                return localArrayList;
            localArrayList.add(new Point(paramPoint.x, i));
            i += paramInt2;
        }
    }

    private int getIndex(int paramInt1, int paramInt2) {
        int i;
        if (paramInt1 % paramInt2 != 0)
            i = paramInt1 / paramInt2;
        else
            i = paramInt1 / paramInt2 - 1;
        return i;
    }

    private List<Point> getLeftChanel(Point paramPoint, int paramInt1, int paramInt2) {
        ArrayList localArrayList = new ArrayList();
        int i = paramPoint.x - paramInt2;
        while (true) {
            if ((i < paramInt1) || (hasPiece(i, paramPoint.y)))
                return localArrayList;
            localArrayList.add(new Point(i, paramPoint.y));
            i -= paramInt2;
        }
    }

    private Map<Point, Point> getLinkPoints(Point paramPoint1, Point paramPoint2, int paramInt1, int paramInt2) {
        Object localObject1 = new HashMap();
        Object localObject6 = getUpChanel(paramPoint1, paramPoint2.y, paramInt2);
        Object localObject5 = getRightChanel(paramPoint1, paramPoint2.x, paramInt1);
        Object localObject3 = getDownChanel(paramPoint1, paramPoint2.y, paramInt2);
        Object localObject7 = getDownChanel(paramPoint2, paramPoint1.y, paramInt2);
        Object localObject4 = getLeftChanel(paramPoint2, paramPoint1.x, paramInt1);
        Object localObject2 = getUpChanel(paramPoint2, paramPoint1.y, paramInt2);
        int j = paramInt2 * (1 + this.config.getYSize()) + this.config.getBeginImageY();
        int i = paramInt1 * (1 + this.config.getXSize()) + this.config.getBeginImageX();
        if ((!isLeftUp(paramPoint1, paramPoint2)) && (!isLeftDown(paramPoint1, paramPoint2))) {
            Map localMap3;
            Object localObject8;
            if (paramPoint1.y == paramPoint2.y) {
                localObject6 = getUpChanel(paramPoint1, 0, paramInt2);
                localObject2 = getUpChanel(paramPoint2, 0, paramInt2);
                localMap3 = getXLinkPoints((List) localObject6, (List) localObject2, paramInt2);
                localObject3 = getDownChanel(paramPoint1, j, paramInt2);
                localObject7 = getDownChanel(paramPoint2, j, paramInt2);
                localObject8 = getXLinkPoints((List) localObject3, (List) localObject7, paramInt2);
                ((Map) localObject1).putAll(localMap3);
                ((Map) localObject1).putAll((Map) localObject8);
            }
            if (paramPoint1.x == paramPoint2.x) {
                localObject5 = getLeftChanel(paramPoint1, 0, paramInt1);
                localObject4 = getLeftChanel(paramPoint2, 0, paramInt1);
                localMap3 = getYLinkPoints((List) localObject5, (List) localObject4, paramInt1);
                localObject5 = getRightChanel(paramPoint1, i, paramInt1);
                localObject8 = getRightChanel(paramPoint2, i, paramInt1);
                localObject8 = getYLinkPoints((List) localObject5, (List) localObject8, paramInt1);
                ((Map) localObject1).putAll(localMap3);
                ((Map) localObject1).putAll((Map) localObject8);
            }
            if (isRightUp(paramPoint1, paramPoint2)) {
                localObject7 = getXLinkPoints((List) localObject6, (List) localObject7, paramInt1);
                localObject6 = getYLinkPoints((List) localObject5, (List) localObject4, paramInt2);
                localObject3 = getUpChanel(paramPoint1, 0, paramInt2);
                localObject2 = getUpChanel(paramPoint2, 0, paramInt2);
                localMap3 = getXLinkPoints((List) localObject3, (List) localObject2, paramInt1);
                localObject3 = getDownChanel(paramPoint1, j, paramInt2);
                localObject4 = getDownChanel(paramPoint2, j, paramInt2);
                localObject8 = getXLinkPoints((List) localObject3, (List) localObject4, paramInt1);
                localObject5 = getRightChanel(paramPoint1, i, paramInt1);
                localObject4 = getRightChanel(paramPoint2, i, paramInt1);
                Map localMap4 = getYLinkPoints((List) localObject5, (List) localObject4, paramInt2);
                Object localObject9 = getLeftChanel(paramPoint1, 0, paramInt1);
                localObject4 = getLeftChanel(paramPoint2, 0, paramInt1);
                localObject9 = getYLinkPoints((List) localObject9, (List) localObject4, paramInt2);
                ((Map) localObject1).putAll((Map) localObject7);
                ((Map) localObject1).putAll((Map) localObject6);
                ((Map) localObject1).putAll(localMap3);
                ((Map) localObject1).putAll((Map) localObject8);
                ((Map) localObject1).putAll(localMap4);
                ((Map) localObject1).putAll((Map) localObject9);
            }
            if (isRightDown(paramPoint1, paramPoint2)) {
                localObject2 = getXLinkPoints((List) localObject3, (List) localObject2, paramInt1);
                localObject3 = getYLinkPoints((List) localObject5, (List) localObject4, paramInt2);
                localObject4 = getXLinkPoints(getUpChanel(paramPoint1, 0, paramInt2),
                        getUpChanel(paramPoint2, 0, paramInt2), paramInt1);
                localObject5 = getXLinkPoints(getDownChanel(paramPoint1, j, paramInt2),
                        getDownChanel(paramPoint2, j, paramInt2), paramInt1);
                Map localMap2 = getYLinkPoints(getLeftChanel(paramPoint1, 0, paramInt1),
                        getLeftChanel(paramPoint2, 0, paramInt1), paramInt2);
                Map localMap1 = getYLinkPoints(getRightChanel(paramPoint1, i, paramInt1),
                        getRightChanel(paramPoint2, i, paramInt1), paramInt2);
                ((Map) localObject1).putAll((Map) localObject2);
                ((Map) localObject1).putAll((Map) localObject3);
                ((Map) localObject1).putAll((Map) localObject4);
                ((Map) localObject1).putAll((Map) localObject5);
                ((Map) localObject1).putAll(localMap2);
                ((Map) localObject1).putAll(localMap1);
            }
            localObject1 = localObject1;
        } else {
            localObject1 = getLinkPoints(paramPoint2, paramPoint1, paramInt1, paramInt2);
        }
        return (Map<Point, Point>) localObject1;
    }

    private List<Point> getRightChanel(Point paramPoint, int paramInt1, int paramInt2) {
        ArrayList localArrayList = new ArrayList();
        int i = paramInt2 + paramPoint.x;
        while (true) {
            if ((i > paramInt1) || (hasPiece(i, paramPoint.y)))
                return localArrayList;
            localArrayList.add(new Point(i, paramPoint.y));
            i += paramInt2;
        }
    }

    private LinkVo getShortcut(Point paramPoint1, Point paramPoint2, Map<Point, Point> paramMap, int paramInt) {
        ArrayList localArrayList = new ArrayList();
        Iterator localIterator = paramMap.keySet().iterator();
        while (localIterator.hasNext()) {
            Point localPoint = (Point) localIterator.next();
            localArrayList.add(new LinkVo(paramPoint1, localPoint, paramMap.get(localPoint), paramPoint2));
        }
        return getShortcut(localArrayList, paramInt);
    }

    private LinkVo getShortcut(List<LinkVo> paramList, int paramInt) {
        int k = 0;
        LinkVo localObject = null;
        for (int i = 0; i < paramList.size(); i++) {
            LinkVo localLinkInfo = paramList.get(i);
            int j = countAll(localLinkInfo.getLinkPoints());
            if (i == 0) {
                k = j - paramInt;
                localObject = localLinkInfo;
            }
            if (j - paramInt >= k)
                continue;
            k = j - paramInt;
            localObject = localLinkInfo;
        }
        return localObject;
    }

    private List<Point> getUpChanel(Point paramPoint, int paramInt1, int paramInt2) {
        ArrayList localArrayList = new ArrayList();
        int i = paramPoint.y - paramInt2;
        while (true) {
            if ((i < paramInt1) || (hasPiece(paramPoint.x, i)))
                return localArrayList;
            localArrayList.add(new Point(paramPoint.x, i));
            i -= paramInt2;
        }
    }

    private Point getWrapPoint(List<Point> paramList1, List<Point> paramList2) {
        int i = 0;
        Point localPoint = null;
        while (i < paramList1.size()) {
            localPoint = paramList1.get(i);
            int j = 0;
            while (j < paramList2.size()) {
                if (!localPoint.equals(paramList2.get(j))) {
                    j++;
                    continue;
                }
                break;
            }
            i++;
        }
        return localPoint;
    }

    private Map<Point, Point> getXLinkPoints(List<Point> paramList1, List<Point> paramList2, int paramInt) {
        HashMap localHashMap = new HashMap();
        for (int j = 0; j < paramList1.size(); j++) {
            Point localPoint2 = paramList1.get(j);
            for (int i = 0; i < paramList2.size(); i++) {
                Point localPoint1 = paramList2.get(i);
                if ((localPoint2.y != localPoint1.y) || (isXBlock(localPoint2, localPoint1, paramInt)))
                    continue;
                localHashMap.put(localPoint2, localPoint1);
            }
        }
        return localHashMap;
    }

    private Map<Point, Point> getYLinkPoints(List<Point> paramList1, List<Point> paramList2, int paramInt) {
        HashMap localHashMap = new HashMap();
        for (int i = 0; i < paramList1.size(); i++) {
            Point localPoint1 = paramList1.get(i);
            for (int j = 0; j < paramList2.size(); j++) {
                Point localPoint2 = paramList2.get(j);
                if ((localPoint1.x != localPoint2.x) || (isYBlock(localPoint1, localPoint2, paramInt)))
                    continue;
                localHashMap.put(localPoint1, localPoint2);
            }
        }
        return localHashMap;
    }

    private boolean hasPiece(int paramInt1, int paramInt2) {
        return findPiece(paramInt1, paramInt2) != null;
    }

    private boolean isLeftDown(Point paramPoint1, Point paramPoint2) {
        return (paramPoint2.x < paramPoint1.x) && (paramPoint2.y > paramPoint1.y);
    }

    private boolean isLeftUp(Point paramPoint1, Point paramPoint2) {
        return (paramPoint2.x < paramPoint1.x) && (paramPoint2.y < paramPoint1.y);
    }

    private boolean isRightDown(Point paramPoint1, Point paramPoint2) {
        return (paramPoint2.x > paramPoint1.x) && (paramPoint2.y > paramPoint1.y);
    }

    private boolean isRightUp(Point paramPoint1, Point paramPoint2) {
        return (paramPoint2.x > paramPoint1.x) && (paramPoint2.y < paramPoint1.y);
    }

    private boolean isXBlock(Point paramPoint1, Point paramPoint2, int paramInt) {
        if (paramPoint2.x >= paramPoint1.x) {
            int i = paramInt + paramPoint1.x;
            while (i < paramPoint2.x) {
                if (!hasPiece(i, paramPoint1.y)) {
                    i += paramInt;
                    continue;
                }
                i = 1;
                return false;
            }
            i = 0;
        }
        return isXBlock(paramPoint2, paramPoint1, paramInt);
    }

    private boolean isYBlock(Point paramPoint1, Point paramPoint2, int paramInt) {
        if (paramPoint2.y >= paramPoint1.y) {
            int i = paramInt + paramPoint1.y;
            while (i < paramPoint2.y) {
                if (!hasPiece(paramPoint1.x, i)) {
                    i += paramInt;
                    continue;
                }
                i = 1;
                return false;
            }
            i = 0;
        }
        return isYBlock(paramPoint2, paramPoint1, paramInt);
    }

    @Override
    public Cut findPiece(float paramFloat1, float paramFloat2) {
        int j = (int) paramFloat1 - this.config.getBeginImageX();
        int i = (int) paramFloat2 - this.config.getBeginImageY();
        Cut localPiece;
        if ((j >= 0) && (i >= 0)) {
            j = getIndex(j, 31);
            i = getIndex(i, 34);
            if ((j >= 0) && (i >= 0)) {
                if ((j < this.config.getXSize()) && (i < this.config.getYSize()))
                    localPiece = this.pieces[j][i];
                else
                    localPiece = null;
            } else
                localPiece = null;
        } else {
            localPiece = null;
        }
        return localPiece;
    }

    @Override
    public Cut[][] getPieces() {
        return this.pieces;
    }

    @Override
    public Cut[] getTwoPieces() {
        int m = 0;
        Cut localObject;
        while (m < this.pieces.length) {
            for (int k = 0; k < this.pieces[m].length; k++) {
                Cut localPiece = this.pieces[m][k];
                Cut[] arrayOfPiece;
                for (int i = 0; i < this.pieces.length; i++) {
                    int j = 0;
                    while (j < this.pieces[i].length) {
                        if ((m != i) || (k != j)) {
                            localObject = this.pieces[i][j];
                            if ((localPiece != null) && (localObject != null)
                                    && (link(localPiece, localObject) != null))
                                ;
                        } else {
                            j++;
                            continue;
                        }
                        arrayOfPiece = new Cut[2];
                        arrayOfPiece[0] = localPiece;
                        arrayOfPiece[1] = localObject;
                        return arrayOfPiece;
                    }
                }
            }
            m++;
            continue;
        }
        return null;
    }

    @Override
    public boolean hasPieces() {
        int i = 0;
        while (i < this.pieces.length) {
            int j = 0;
            while (j < this.pieces[i].length) {
                if (this.pieces[i][j] == null) {
                    j++;
                    continue;
                }
                return true;
            }
            i++;
        }
        return false;
    }

    @Override
    public LinkVo link(Cut paramPiece1, Cut paramPiece2) {
        Object localObject1;
        if (!paramPiece1.equals(paramPiece2)) {
            if (paramPiece1.isSameImage(paramPiece2)) {
                if (paramPiece2.getIndexX() >= paramPiece1.getIndexX()) {
                    Point localPoint = paramPiece1.getCenter();
                    localObject1 = paramPiece2.getCenter();
                    if ((paramPiece1.getIndexY() != paramPiece2.getIndexY())
                            || (isXBlock(localPoint, (Point) localObject1, 31))) {
                        if ((paramPiece1.getIndexX() != paramPiece2.getIndexX())
                                || (isYBlock(localPoint, (Point) localObject1, 34))) {
                            Object localObject2 = getCornerPoint(localPoint, (Point) localObject1, 31, 34);
                            if (localObject2 == null) {
                                localObject2 = getLinkPoints(localPoint, (Point) localObject1, 31, 34);
                                if (((Map) localObject2).size() == 0)
                                    localObject1 = null;
                                else
                                    localObject1 = getShortcut(localPoint, (Point) localObject1, (Map) localObject2,
                                            getDistance(localPoint, (Point) localObject1));
                            } else {
                                localObject1 = new LinkVo(localPoint, (Point) localObject2, (Point) localObject1);
                            }
                        } else {
                            localObject1 = new LinkVo(localPoint, (Point) localObject1);
                        }
                    } else
                        localObject1 = new LinkVo(localPoint, (Point) localObject1);
                } else {
                    localObject1 = link(paramPiece2, paramPiece1);
                }
            } else
                localObject1 = null;
        } else
            localObject1 = null;
        return (LinkVo) localObject1;
    }

    @Override
    public void refreshPieces() {
        this.pieces = this.board.reCreatePieces(this.config, this.pieces);
    }

    @Override
    public void setConfig(GameConfig paramGameConf) {
        this.config = paramGameConf;
    }

    @Override
    public void start() {
        this.board = new Board();
        this.pieces = this.board.create(this.config);
    }
}